package hyh.com.videodemo;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.util.Log;
import android.view.Surface;

public class SimplePlayer {

    static{
        System.loadLibrary("native-lib");
    }

    private AudioTrack audioTrack;
    //    这个方法  是C进行调用  通道数
    private void createTrack(int sampleRateInHz,int nb_channals) {
        Log.e("androidLog",sampleRateInHz+":"+nb_channals);
        int channaleConfig;//通道数
        if (nb_channals == 1) {
            channaleConfig = AudioFormat.CHANNEL_OUT_MONO;
        } else if (nb_channals == 2) {
            channaleConfig = AudioFormat.CHANNEL_OUT_STEREO;
        }else {
            channaleConfig = AudioFormat.CHANNEL_OUT_MONO;
        }
        int buffersize= AudioTrack.getMinBufferSize(sampleRateInHz,
                channaleConfig, AudioFormat.ENCODING_PCM_16BIT);
        audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,sampleRateInHz,channaleConfig,
                AudioFormat.ENCODING_PCM_16BIT,buffersize, AudioTrack.MODE_STREAM);
        audioTrack.play();
    }

    //C传入音频数据
    private void playTrack(byte[] buffer, int lenth) {

        try{
            if (audioTrack != null && audioTrack.getPlayState() == AudioTrack.PLAYSTATE_PLAYING) {
                audioTrack.write(buffer, 0, lenth);
            }
        }catch (Exception e){
        }

    }

    public native void start(String filePath, Surface surface);

    public native void putData(byte data[],int len);

    public native void stop();
}
